package demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public enum TestOne {
    RED("red",1),BLUE("blue",2),BLACK("black",3);
    private String name;
    private int key;
    /**
     * 当枚举对象有了构造方法时，我们添加枚举常量需要给它赋值*/
    private TestOne(String name,int key) {
        this.name = name;
        this.key = key;
    }
}
class reflectEnum {
    public static void reflect(){
        try {
            Class<?> classEnum = Class.forName("demo1.TestOne");

            //使用class对象获得类的构造方法
            Constructor classConstructor = classEnum.getDeclaredConstructor(String.class,int.class,String.class,int.class);

            //将修改权限设置为true
            classConstructor.setAccessible(true);

            //传参创建实例对象
            Object objectEnum = classConstructor.newInstance("PURPLE",5);

            //打印对象
            System.out.println(objectEnum);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        reflect();
    }
}
